<template>
    <section>
        <!--工具条-->
        <div class="section-content pb-top clearfix">
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">

                    <el-form-item>
                        <el-select v-model="filters.deviceId" filterable  placeholder="设备" @change="getUsers()">
                            <!-- <el-option  key="" label="所有" value=""></el-option> -->
                            <el-option v-for="item in devicelist" :key="item.deviceId" :label="item.name+'('+item.deviceSn+')'" :value="item.deviceId">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-select v-model="filters.type" placeholder="告警处理状态" @change="getUsers()">
                            <el-option key="" label="发送接收" value=""></el-option>
                            <el-option key="0" label="发送" value="0"></el-option>
                            <el-option key="1" label="接收" value="1"></el-option>
                            
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item>

                        <el-select v-model="filters.eventCode" placeholder="eventCode" @change="getUsers()">
                            <el-option key=" " label="全部" value=" "></el-option>
                            <el-option v-for="item in eventCode" :key="item.value" :label="item.remark" :value="item.value"></el-option>
                            
                        </el-select>
                    </el-form-item> -->
                    <el-form-item>
                        <el-date-picker v-model="value4" @change="second1()" type="datetimerange" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions2">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>

                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="query" highlight-current-row class="alarmshistory" :header-row-style="{backgroundColor: titleColor}" v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
                <!-- <el-table-column type="selection">
            </el-table-column> -->
                <el-table-column type="index" label="序号" width="80" :index="indexMethod">
                </el-table-column>
                <el-table-column prop="deviceSn" align="center" label="网关SN">
                </el-table-column>
                <el-table-column prop="createTime" align="center" label="时间">
                </el-table-column>
                <!-- <el-table-column prop="eventCode" label="eventCode" >
            </el-table-column> -->
                <el-table-column prop="content" align="center" width="600" label="内容">
                </el-table-column>
                <el-table-column prop="type" align="center" width="100" label="类型" :formatter="formattype">
                </el-table-column>
                <!-- <el-table-column prop="state" label="处理状态" sortable>
                </el-table-column>
                <el-table-column label="操作">
                    <template scope="scope">
    
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)" title="编辑" icon="el-icon-edit"
                            circle></el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete"
                            circle title="删除"></el-button>
                    </template>
                </el-table-column> -->
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                    style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="处理告警" v-show="editFormVisible" :custom-class="dialogTitleColor" v-model="editFormVisible" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="处理状态">
                        <el-radio-group v-model="editForm.state">
                            <el-radio label="0">未处理</el-radio>
                            <el-radio label="1">已处理</el-radio>
                            <el-radio label="2">误报</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="处理结果">
                        <el-input type="textarea" v-model="editForm.remark"></el-input>

                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="editLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" :custom-class="dialogTitleColor" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="名字" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="电话号码" prop="name">
                        <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="email" prop="name">
                        <el-input v-model="addForm.email" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="微信" prop="name">
                        <el-input v-model="addForm.weixin" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input type="textarea" v-model="addForm.remark"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        alarm_log_query,
        alarms_query_user,
        alarms_post_user,
        alarms_delete_user,
        alarms_put_user,
        alarm_log_get,
        alarm_log_recover,
        device_query,
        alarm_type_query,
        device_get,
        device_event_query,
        dict_query
    } from '../../api/api';

    export default {
        data() {
            return {
                pickerOptions2: {
                    shortcuts: [{
                        text: '最近1小时',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 1);
                            picker.$emit('pick', [start, end]);
                        }
                    },{
                        text: '最近24小时',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                devicelist: '',
                deviceSlavelist: '',
                alarmtype: '',
                value4: [new Date().getTime() - 3600 * 1000 * 24 * 1, new Date()],
                filters: {
                    deviceId: '',
                    // deviceSlaveId: '',
                    // alarmTypeId: '',
                    // state: '',
                    startTime: '',
                    endTime: '',
                    type: '',
                    eventCode: ''

                },
                eventCode: '',
                query: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列

                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                    state: '',
                    remark: '',

                },

                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    path: [{
                            required: true,
                            message: '请输入path',
                            trigger: 'blur'
                        }

                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    phone: '',
                    email: '',
                    weixin: '',
                    remark: ''
                }

            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            getdata(date) {

                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                m = m < 10 ? ('0' + m) : m;
                var d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                var h = date.getHours();
                h = h < 10 ? ('0' + h) : h;
                var minute = date.getMinutes();
                minute = minute < 10 ? ('0' + minute) : minute;
                var second = date.getSeconds();
                second = second < 10 ? ('0' + second) : second;
                return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;

            },
            //性别显示转换
            second1() {

                this.filters.startTime = this.getdata(this.value4[0]);
                this.filters.endTime = this.getdata(this.value4[1]);
            },
            second() {
                if (this.filters.deviceId) {
                    let para = {
                        deviceId: this.filters.deviceId,
                    };
                    device_get(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {
                            //NProgress.done();
                            this.deviceSlavelist = res.data.deviceSlaveList;
                            console.log(this.deviceSlavelist)

                        }

                    });
                } else {
                    this.deviceDataRecipeBody = {};
                }
            },
            formattype(row, column) {

                return row.type == 1 ? '接收' : row.type == 0 ? '发送' : '未知';
            },
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取菜单列表
            getUsers() {
                if (this.menubt.indexOf('/device_event/query') == -1) {
                    this.$message({
                        message: '您没有查询设备日志的权限',
                        type: 'warning'
                    });
                    return false;
                }
                if (!this.filters.deviceId) {
                    this.$message({
                        message: '请先选择设备',
                        type: 'warning'
                    });
                    return false;
                }
                let para = {

                    pageNo: this.page,
                    pageSize: 10,
                    type: this.filters.type,
                    deviceId: this.filters.deviceId,
                    eventCode: this.filters.eventCode,
                    // deviceSlaveId:this.filters.deviceSlaveId,
                    // alarmTypeId:this.filters.alarmTypeId,
                    // state:this.filters.state,
                    startTime: this.filters.startTime,
                    endTime: this.filters.endTime

                };
                // this.listLoading = true;
                //NProgress.start();
                device_event_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.total = res.data.totalCount;
                    this.query = res.data.list;
                    this.listLoading = false;
                    this.page=res.data.pageNo;
                    //NProgress.done();
                });


            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        id: row.id
                    };
                    alarms_delete_user(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {

                let para = {
                    id: row.id
                };

                alarm_log_get(para).then((res) => {
                     this.begCode(res.code,res.message);

                    if (res.code == 0) {
                        this.editLoading = false;
                        //NProgress.done();
                        this.editFormVisible = true;
                        this.editForm.id = res.data.id;
                        this.editForm.remark = res.data.remark;
                        this.editForm.state = res.data.state.toString();
                    }
                });

            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    sex: -1,
                    age: 0,
                    birth: '',
                    addr: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);

                            alarm_log_recover(para).then((res) => {
                                 this.begCode(res.code,res.message);

                                if (res.code == 0) {
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);

                            alarms_post_user(para).then((res) => {
                                 this.begCode(res.code,res.message);
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        idList: ids.split(',')
                    };
                    alarms_delete_user(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.menubt = localStorage.getItem('menubt');
            let start1 = new Date();
            let start2 = new Date();
            start1.setTime(start1.getTime() - 3600 * 1000 * 1 * 1);
            start2.setTime(start2.getTime());

            this.value4 = [this.getdata(start1), this.getdata(start2)];
            this.filters.startTime = this.getdata(start1);
            this.filters.endTime = this.getdata(start2);

            // this.getUsers();
            let para = {
                pageNo: this.page,
                pageSize: 9999,
                miniLev:2

            };
            //NProgress.start();
            device_query(para).then((res) => {
                 this.begCode(res.code,res.message);
                this.devicelist = res.data.list;
                //NProgress.done();
            });
            let para1 = {
                name: 'eventCode',
                pageSize: 999,
                pageNo: 1,
            }

            dict_query(para1).then((res) => {
                 this.begCode(res.code,res.message);
                if (res.code == 0) {
                    this.eventCode = res.data.list;


                }

            });

        }
    }
</script>

<style scoped>

</style>